package com.kpj.widjet;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.EditText;

public class Editor extends EditText {
	
	private final int LEFT_MARGIN = 20;
	private final int PADDING = 10;
	private final int LINE_NUM_LEFT_MARGIN = 15;
	
	private SyntaxListener syntaxListener;

	Paint backgroundPaint;
	Paint textPaint;
	
	public Editor(Context context) {
		super(context);
		init();
	}
	
	public Editor(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	private void init(){
		backgroundPaint = new Paint();
		backgroundPaint.setColor(Color.LTGRAY);
		textPaint = new Paint();
		textPaint.setColor(Color.RED);
		textPaint.setAntiAlias(true);
		
	}
	
	public void setSyntaxListener(SyntaxListener listener) {
		syntaxListener = listener;
	}
	
	public void checkSyntax() {
		if (syntaxListener != null)
			syntaxListener.onCheck();
	}
	
	public interface SyntaxListener {
		public boolean onCheck();
	}

	
	
	@Override
	protected void onDraw(Canvas canvas) {
		int lineHeight = getLineHeight();
		for (int i = 0; i < getLineCount(); i++) {
			canvas.drawText(""+(i+1), LINE_NUM_LEFT_MARGIN, i*lineHeight+lineHeight+((lineHeight-getTextSize())/2), textPaint);
	}
		canvas.drawRect(canvas.getClipBounds().left+LEFT_MARGIN+PADDING, canvas.getClipBounds().top+PADDING, canvas.getClipBounds().right-PADDING, canvas.getClipBounds().bottom-PADDING,backgroundPaint);
		this.setPadding(LEFT_MARGIN+LEFT_MARGIN,PADDING,PADDING,PADDING);
		super.onDraw(canvas);
	}
	

	
	

}
